package com.finance.client;

import com.finance.util.CommonUtil;
import com.finance.util.DataType;
import com.finance.util.SearchDateType;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class FinanceApiClient {

    @Value("${finance.api.host}")
    private String host;

    @Value("${finance.api.client-id}")
    private String clientId;

    @Value("${finance.api.client-secret}")
    private String clientSecret;

    private final ApiCallService apiCallService;

    public FinanceApiClient(ApiCallService apiCallService) {
        this.apiCallService = apiCallService;
    }

    private static final String NAMESPACE = "alibaba.dme.lark";
    private static final String PROTOCOL = "param2";
    private static final int VERSION = 1;

    public String callApi(DataType dataType, String searchDate, SearchDateType searchDateType, String cinemaLinkIds) {
        String urlHead = "https://" + host + "/openapi/";
        String name = "dme.lark.data.finance.getFinancialData";

        Map<String, String> params = new HashMap<>();
        params.put("client_id", clientId);
        params.put("client_secret", clientSecret);
        params.put("leaseCode", "empc");
        params.put("channelCode", "EMPC_OPEN");
        params.put("cinemaLinkIds", cinemaLinkIds);
        params.put("dataType", dataType.getCode());
        params.put("searchDateType", searchDateType.getCode());
        params.put("searchDate", searchDate);
        params.put("_aop_timestamp", String.valueOf(System.currentTimeMillis()));
        String urlPath = CommonUtil.buildInvokeUrlPath(NAMESPACE, name, VERSION, PROTOCOL, clientId);
        params.put("_aop_signature", CommonUtil.signatureWithParamsAndUrlPath(urlPath, params, clientSecret));
//        return apiCallService.callApi(urlHead, urlPath, params);
        return com.finance.service.ApiCallService.callApiTest(urlHead, urlPath, clientSecret, params);
    }
} 